package com.gintama.common.error;


import static com.gintama.base.enums.BaseResultCode.ERROR_CODE;

import com.gintama.base.enums.BaseResultCode;
import java.io.Serializable;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

/**
 * @author zhenyao.zhao
 * @since 2020/3/18
 */
@Data
@ToString
@EqualsAndHashCode(callSuper = true)
public class BizException extends RuntimeException implements Serializable {

    private static final long serialVersionUID = 8700683825388813199L;
    private String code;
    private String msg;

    private BizException() {
    }

    private BizException(String code, String msg) {
        super(msg);
        this.code = code;
        this.msg = msg;
    }

    private BizException(String code, String msg, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
        this.msg = msg;
    }

    public static BizException empty() {
        return new BizException();
    }

    public static BizException of(BaseResultCode resultCode) {
        return of(resultCode, resultCode.msg());
    }

    public static BizException of(BaseResultCode resultCode, String msg) {
        if (resultCode == null) {
            throw empty();
        }
        return of(resultCode.code(), msg);
    }

    public static BizException of(String code, String msg) {
        return new BizException(code, msg);
    }

    public static BizException of(String code, String msg, Throwable cause) {
        return of(code, msg, msg, cause);
    }

    public static BizException of(String code, String msg, String message, Throwable cause) {
        return new BizException(code, msg, message, cause);
    }

    public static BizException ofCode(String code) {
        return of(code, "com.xfs.common.error.BizException");
    }

    public static BizException ofMsg(String msg) {
        return of(ERROR_CODE, msg);
    }

}
